package cn.wolfcode.p2p.base.util;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.vo.VerifyCodeVo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取session
 */
public class UserContext {

    public static final String USER_IN_SESSION = "loginInfo"; //登录信息
    public static final String VERIFY_IN_SESSION = "verifycode"; //验证码

    public static HttpServletRequest getRequest(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    public static void setCurrent(LoginInfo loginInfo){
        getRequest().getSession().setAttribute(USER_IN_SESSION, loginInfo);
    }

    public static LoginInfo getCurrent(){
        return (LoginInfo) getRequest().getSession().getAttribute(USER_IN_SESSION);
    }

    public static void setVerifyCodeVo(VerifyCodeVo vo){
        getRequest().getSession().setAttribute(VERIFY_IN_SESSION, vo);
    }

    public static VerifyCodeVo getVerifyCodeVO(){
        return (VerifyCodeVo) getRequest().getSession().getAttribute(VERIFY_IN_SESSION);
    }

    public static String getIp() {
        return getRequest().getRemoteAddr();
    }
}
